package com.web.servlet;

import java.util.Map;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.web.constant.Constant;
import com.web.utils.Utils;
import com.web.vo.ResultVO;

@WebServlet("/login/*")
public class LoginServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;

	@Override
	public boolean before(HttpServletRequest request, HttpServletResponse response, Map<String, Object> data) {
		return true;
	}

	public ResultVO login(HttpServletRequest request, HttpServletResponse response, Map<String, Object> data) {
		ResultVO res = new ResultVO();
		HttpSession session = request.getSession();
		String code = (String)data.get("code");
		String md5_code=Utils.getMD5(Utils.getMD5(Constant.code));
		if (code.equals(md5_code)) {
			session.setAttribute(Constant.ManagerSessionName,"已登录");
			res.setMessage("登录成功");
			res.setStatus(Constant.SUCCESS_STATUS);
		}else {
			res.setStatus(Constant.ERROR_STATUS);
			res.setMessage("登录失败");
		}
		return res;
	}

	public ResultVO checkLogin(HttpServletRequest request, HttpServletResponse response, Map<String, Object> data) {
		ResultVO res = new ResultVO();
		HttpSession session = request.getSession();
		if (session.getAttribute(Constant.ManagerSessionName)==null) {
			res.setMessage("管理员未登录");
			res.setStatus(Constant.ISNOT_LOGIN);
		} else {
			res.setStatus(Constant.SUCCESS_STATUS);
			res.setMessage("管理员已登录");
		}
		return res;
	}
	
	
}
